package com.zh.kotlin_mvvm.ui

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.core.view.isVisible
import androidx.core.widget.doAfterTextChanged
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.KeyboardUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.vanniktech.ui.smoothScrollTo
import com.zh.common.base.BaseActivity
import com.zh.kotlin_mvvm.R
import com.zh.kotlin_mvvm.databinding.ActivityChatBinding
import com.zh.kotlin_mvvm.fragment.EmojiFragment
import com.zh.kotlin_mvvm.fragment.InputExpandFragment

class ChatActivity : BaseActivity<ActivityChatBinding>() {

    private val emojiFragment by lazy { EmojiFragment() }
    private val inputExpandFragment by lazy { InputExpandFragment() }

    private val iAdapter by lazy {
        object : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_chat) {
            override fun convert(holder: BaseViewHolder, item: String) {
                holder.setText(R.id.emojiView, item)
            }
        }
    }

    override val interceptKeyBoard: Boolean
        get() = false

    @SuppressLint("ClickableViewAccessibility")
    override fun initView(savedInstanceState: Bundle?) {
        viewBinding.fragmentContainer.isVisible = false
        viewBinding.fragmentContainerEmoji.isVisible = false
        viewBinding.recyclerView.adapter = iAdapter

        viewBinding.recyclerView.setOnTouchListener { v, event ->
            KeyboardUtils.hideSoftInput(this)
            viewBinding.fragmentContainer.isVisible = false
            viewBinding.fragmentContainerEmoji.isVisible = false
            false
        }

        viewBinding.chatMoreOrSend.setOnClickListener {
            if (viewBinding.chatMoreOrSend.isSelected) {//发送
                val text = viewBinding.chatInput.text.toString()
                if (!TextUtils.isEmpty(text)) {
                    updateListAdapter(text)
                    viewBinding.chatInput.setText("")
                    viewBinding.chatMoreOrSend.isSelected = false
                }
            } else {
                viewBinding.voice.isSelected = false
                viewBinding.emoji.isSelected = false
                viewBinding.chatMoreOrSend.isSelected = false
                if (KeyboardUtils.isSoftInputVisible(this)) {
                    clearFocus()
                    KeyboardUtils.hideSoftInput(this)
                    it.postDelayed({
                        viewBinding.fragmentContainer.isVisible = true
                        viewBinding.fragmentContainerEmoji.isVisible = false
                    }, 100)
                } else {
                    viewBinding.fragmentContainer.isVisible = true
                    viewBinding.fragmentContainerEmoji.isVisible = false
                }
            }
        }

        viewBinding.chatInput.doAfterTextChanged {
            viewBinding.chatMoreOrSend.isSelected = !it.isNullOrEmpty()
        }

        viewBinding.chatInput.setOnClickListener {
            viewBinding.emoji.isSelected = false
            viewBinding.fragmentContainer.isVisible = false
            viewBinding.fragmentContainerEmoji.isVisible = false
        }

        viewBinding.chatInput.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                viewBinding.emoji.isSelected = false
                viewBinding.fragmentContainer.isVisible = false
                viewBinding.fragmentContainerEmoji.isVisible = false
            }
        }

        viewBinding.voice.setOnCheckedChangeListener { v, isChecked ->
            clearFocus()
            KeyboardUtils.hideSoftInput(this)
            viewBinding.fragmentContainer.isVisible = false
            viewBinding.fragmentContainerEmoji.isVisible = false
            viewBinding.emoji.isSelected = false
            if (isChecked) {
                viewBinding.inputLy.isVisible = false
                viewBinding.touchSay.isVisible = true
            } else {
                viewBinding.inputLy.isVisible = true
                viewBinding.touchSay.isVisible = false
            }
        }

        viewBinding.emoji.setOnClickListener { v ->
            viewBinding.voice.isChecked = false
            if (v.isSelected) { //键盘
                KeyboardUtils.showSoftInput(this)
                viewBinding.fragmentContainer.isVisible = false
                viewBinding.fragmentContainerEmoji.isVisible = false
                v.isSelected = false
            } else { //表情
                if (KeyboardUtils.isSoftInputVisible(this)) {
                    clearFocus()
                    KeyboardUtils.hideSoftInput(this)
                    v.postDelayed({
                        viewBinding.fragmentContainer.isVisible = false
                        viewBinding.fragmentContainerEmoji.isVisible = true
                    }, 100)
                } else {
                    viewBinding.fragmentContainer.isVisible = false
                    viewBinding.fragmentContainerEmoji.isVisible = true
                }
                v.isSelected = true
            }
        }

        emojiFragment.editView = viewBinding.chatInput
        supportFragmentManager.beginTransaction()
            .add(viewBinding.fragmentContainerEmoji.id, emojiFragment)
            .commit()

        supportFragmentManager.beginTransaction()
            .add(viewBinding.fragmentContainer.id, inputExpandFragment)
            .commit()


        viewBinding.recyclerView.addOnLayoutChangeListener { v, i, i1, i2, i3, i4, i5, i6, i7 ->
            if (i3 < i7) { // bottom < oldBottom
                viewBinding.recyclerView.smoothScrollTo(0)
            }
        }
    }

    private fun updateListAdapter(src: String) {
        iAdapter.addData(0, src)
        viewBinding.recyclerView.smoothScrollTo(0)
    }

    private fun clearFocus() {
        viewBinding.chatInput.clearFocus()
    }

    override fun onDestroy() {
        supportFragmentManager.beginTransaction().remove(emojiFragment)
        supportFragmentManager.beginTransaction().remove(inputExpandFragment)
        super.onDestroy()
    }

}